<?php

//require_once "Mail.php";
//require_once('class.phpmailer.php');
if (!defined('DATABASE_NAME')) {
    define("DATABASE_NAME", "default");
}

class User_model extends CI_Model {

    var $id;
    var $name;
    var $login;
    var $password;
    var $token;
    var $DBControle;

    function __construct($id = '', $name = '', $login = '', $password = '', $token = '') {
        parent::__construct();

        $this->id = $id;
        $this->name = $name;
        $this->login = $login;
        $this->password = $password;
        $this->token = $token;
    }

    function id($id) {
        $this->DBControle = $this->load->database(DATABASE_NAME, TRUE);

        $this->DBControle->where('u0_id', $id);
        $this->DBControle->where('u0_active', true);
        $query = $this->DBControle->get('User');
        $result = $query->result();

        if (count($result) > 0) {
            $user = $result[0];

            $ret = new User_model($user->u0_id, $user->u0_name, $user->u0_login, $user->u0_password, $user->u0_token);

            return $ret;
        } else {
            return false;
        }
    }

    function login($login) {
        $this->DBControle = $this->load->database(DATABASE_NAME, TRUE);

        $this->DBControle->where('u0_login', $login);
        $this->DBControle->where('u0_active', true);
        $query = $this->DBControle->get('User');
        $result = $query->result();

        if (count($result) > 0) {
            $user = $result[0];

            $this->id = $user->u0_id;
            $this->name = $user->u0_name;
            $this->login = $user->u0_login;
            $this->password = $user->u0_password;

            return $this;
        } else {
            return false;
        }
    }

    function add() {
        $this->DBControle = $this->load->database(DATABASE_NAME, TRUE);
        $insert["u0_name"] = $this->name;
        $insert["u0_login"] = $this->login;
        $insert["u0_password"] = $this->password;
        $insert["u0_token"] = $this->token;

        if (true) {
            $this->DBControle->insert('User', $insert);
            return $this->DBControle->insert_id();
        } else {
            return false;
        }
    }

    function update($id, $name = false, $login = false, $password = false) {
        $this->DBControle = $this->load->database(DATABASE_NAME, TRUE);
        $update = array();

        if ($name) {
            $update["u0_name"] = $name;
        }
        if ($login) {
            $update["u0_login"] = $login;
        }
        if ($password) {
            $update["u0_password"] = $password;
        }

        if (count($update) > 0) {
            $this->DBControle->where('u0_id', $id);
            $this->DBControle->update('User', $update);
        }
    }

    function delete() {
        $this->DBControle = $this->load->database(DATABASE_NAME, TRUE);
    }

    function ls() {
        $this->DBControle = $this->load->database(DATABASE_NAME, TRUE);
        $query3 = $this->DBControle->get('User');
        $result = $query3->result();
        $lista = array();
        foreach ($result as $key => $value) {
            $lista[] = new User_model($value->u0_id, $value->u0_name, $value->u0_login, $value->u0_password);
        }

        return $lista;
    }

    function paginate($page, $pagesize = 15) {
        $this->DBControle = $this->load->database(DATABASE_NAME, TRUE);
        $query3 = $this->DBControle->get('User');
        $result = $query3->result();
        $list = array();
        foreach ($result as $key => $value) {
            $list[] = new User_model($value->u0_id, $value->u0_name, $value->u0_login, $value->u0_password);
        }

        $pagelist = array_slice($list, $page, $pagesize);

        $retorno = array();
        $retorno[0] = count($list);
        $retorno[1] = $pagelist;

        return $retorno;
    }

    function activate($token = '') {
        $this->DBControle = $this->load->database(DATABASE_NAME, TRUE);
        $update = array();

        if ($token != '') {
            $this->DBControle->where('u0_token', $token);
            $this->DBControle->where('u0_active', false);
            $query = $this->DBControle->get('User');
            $result = $query->result();
            if (count($result) == 1) {
                $user = $result[0];
                $this->id = $user->u0_id;
                $this->name = $user->u0_name;
                $this->login = $user->u0_login;
                $this->password = $user->u0_password;
            } else {
                return false;
            }
        }

        $update["u0_active"] = true;

        if ($this->id) {
            $this->DBControle->where('u0_id', $this->id);
            $this->DBControle->update('User', $update);
        }
    }

    function deactivate() {
        $this->DBControle = $this->load->database(DATABASE_NAME, TRUE);
        $update = array();

        if ($name) {
            $update["u0_active"] = false;
        }
        if (count($update) > 0) {
            $this->DBControle->where('u0_id', $this->id);
            $this->DBControle->update('User', $update);
        }
    }

    function printUserData() {
        $this->DBControle = $this->load->database(DATABASE_NAME, TRUE);

        if ($loginid = $this->session->userdata('loginid')) {
//            $this->session->userdata('loginid');
            $ret = "";

//            $this->id($loginid);

            if ($user = $this->id($loginid)) {

                $ret .= "<p>";
                $ret .= "Usuário: " . $user->name . "<br>\n";
                $ret .= "<a href='/logout'>Sair</a>\n";
                $ret .= "</p>";

                return $ret;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

}

?>
